package com.tanhua.common.utils;

import com.tanhua.common.pojo.User;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.core.RedisTemplate;

import java.text.SimpleDateFormat;
import java.util.BitSet;
import java.util.Date;
public class Bitmaps {
    private static final String activeName = "activeUser:";
    //位图法存登录记录
    public void bitmaps(RedisTemplate redisTemplate, User user){
        Date currentTime = new Date();
        String currentStr = new SimpleDateFormat("yyyy-MM-dd").format(currentTime);
        String key = currentStr+activeName;
        //偏移量
        Long offset = user.getId();
        redisTemplate.opsForValue().setBit(key, offset, true);
    }
    public Integer getActiveByDay(RedisTemplate redisTemplate,User user,Integer count){
        Date currentTime = new Date();
        BitSet all = new BitSet();
        for(int i=0;i<count;i++){
            //获得前一天的日期
            currentTime= DateUtils.addDays(currentTime, -1);
            String currentStr = new SimpleDateFormat("yyyy-MM-dd").format(currentTime);
            String key = activeName+currentStr;
            //获得这一天日期的活跃用户字节数组
            /*redisTemplate.opsForValue().get();*/
            //有可能当天没有一个用户登录
            /*if(loginByte!=null){
                BitSet user = BitSet.valueOf(loginByte);
                //取其并集
                all.or(user);
            }*/
        }
        //统计人数
        int coun = all.cardinality();
        return coun;
    }
}
